package com.yyy;

import com.yyy.example.entity.User01;
import com.yyy.modules.MsgTemplate;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableAsync;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


/**
 * @author com.yyy
 * @Date 2020-09-12 15:44
 * @Email yangyouyuhd@163.com
 */
@EnableAsync
@SpringBootApplication
public class MyRabbitAPP { // implements ApplicationRunner {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(MyRabbitAPP.class, args);
        System.out.println("\n\n -- SUCCESS -- \n\n");

//        thread();

    }

    private static void thread() {
        new Thread(() -> {
            lock.lock();
            try {
                System.out.println("开始睡眠：" + new SimpleDateFormat("HH:mm:ss").format(new Date()));
                while (flag) {
                    condition.await(10, TimeUnit.SECONDS);
                }
                System.out.println("结束睡眠：" + new SimpleDateFormat("HH:mm:ss").format(new Date()));
            } catch (Exception e) {
            } finally {
                lock.unlock();
            }
        }).start();


        new Thread(() -> {
            lock.lock();
            try {
                TimeUnit.SECONDS.sleep(2);
                flag = false;
                condition.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }).start();
    }

    static volatile boolean flag = true;
    static Lock lock = new ReentrantLock(false);
    static Condition condition = lock.newCondition();



    @Resource
    MsgTemplate msgTemplate;



//    @Override
    public void run(ApplicationArguments args) throws Exception {
        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(4);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            msgTemplate.send("queue04",new User01());
        }).start();
    }
}


